package sncf.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import sncf.server.EmailService;

public class AdminUtils {

	public static void notifySystemAdmin(Exception e) {
		System.out.println("Notifying Admin...");
		final String ln = System.getProperty("line.separator");
		final String adminEmail = "thierry.moraisdacruz@gmail.com";

		final String subject = "[SNCF DUMPER] Notification d'erreur";
		String message = "/*** MESSAGE AUTOMATIQUE DU SYSTEME D'ALERTE SNCF DUMPER. NE PAS REPONDRE. ***/"
				+ ln + ln;

		InetAddress localHost = null;
		try {
			localHost = Inet6Address.getLocalHost();
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		}
		message += "The following exception occured on SNCF DUMPER server "
				+ (localHost == null ? ":" : localHost.getHostName() + "/"
						+ localHost.getHostAddress()) + " :" + ln + ln;

		StringWriter stringWriter = new StringWriter();
		PrintWriter printWriter = new PrintWriter(stringWriter);

		e.printStackTrace(printWriter);
		StringBuffer buffer = stringWriter.getBuffer();
		message += (buffer == null ? "" : buffer.toString());

		EmailService emailService = EmailService.getInstance();
		List<String> toList = new ArrayList<String>();
		toList.add(adminEmail);
		boolean htmlMessage = false;
		emailService
				.sendSimpleMail(toList, null, subject, message, htmlMessage);
	}

	public static void notifySystemAdmin(String fileName, Exception e) {
		System.out.println("Notifying Admin...");
		final String ln = System.getProperty("line.separator");
		final String adminEmail = "thierry.moraisdacruz@gmail.com";

		final String subject = "[SNCF DUMPER] Notification d'erreur";
		String message = "/*** MESSAGE AUTOMATIQUE DU SYSTEME D'ALERTE SNCF DUMPER. NE PAS REPONDRE. ***/"
				+ ln + ln;

		InetAddress localHost = null;
		try {
			localHost = Inet6Address.getLocalHost();
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		}
		message += "The following exception occured on SNCF DUMPER server "
				+ (localHost == null ? ":" : localHost.getHostName() + "/"
						+ localHost.getHostAddress()) + " :" + ln + ln;

		StringWriter stringWriter = new StringWriter();
		PrintWriter printWriter = new PrintWriter(stringWriter);

		e.printStackTrace(printWriter);
		StringBuffer buffer = stringWriter.getBuffer();
		message += (buffer == null ? "" : buffer.toString());

		EmailService emailService = EmailService.getInstance();
		List<String> toList = new ArrayList<String>();
		toList.add(adminEmail);
		boolean htmlMessage = false;
		List<String> filePathList = new ArrayList<String>();
		filePathList.add(fileName);
		emailService.sendMailWithAttachement(toList, null, subject, message,
				htmlMessage, filePathList);
	}

}
